package com.gt.ao.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Hashtable;
import java.util.List;
import java.util.SortedMap;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.encoder.QRCode;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

/**
 * @author gt.ao 
 * @description //TODO
 * @createTime  2022/7/11 15:59
**/
public class QRUtil {
	public static void main(String[] args) {
		File file = new File("/Users/gt.ao/Desktop/qr1.png");
		File content = new File("/Users/gt.ao/Desktop/content.txt");
		encode(content, file,"content.txt".length());
		//decode(file.getAbsolutePath());
	}
 
	// qrcode 编码
	public static void encode(File file, File QRCode,int byteLen) {
		String content = null;
		try(FileInputStream fileInputStream = new FileInputStream(file)){
			byte[] b = new byte[1010+byteLen];
			fileInputStream.read(b);
			content = new String(b);
		}catch (Exception e){}

		Charset charset = Charset.forName("UTF-8");
		CharsetEncoder encoder = charset.newEncoder();
		byte[] b = null;
		try { // Convert a string to ISO-8859-1 bytes in a ByteBuffer
			System.out.println("-------->" + content.length());
			ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(content));
			b = bbuf.array();
		} catch (CharacterCodingException e) {
			System.out.println(e.getMessage());
		}
		String data = "";
		try {
			data = new String(b, "iso8859-1");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} // get a byte matrix for the data
		BitMatrix matrix = null;
		int h = 900;
		int w = 800;
		com.google.zxing.Writer writer = new QRCodeWriter();
		try {
			matrix = writer.encode(data,
					com.google.zxing.BarcodeFormat.QR_CODE, w, h);
		} catch (com.google.zxing.WriterException e) {
			System.out.println(e.getMessage());
		}
		try {
			MatrixToImageWriter.writeToFile(matrix, "PNG", QRCode);
			System.out.println("printing to " + QRCode.getAbsolutePath());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	/**
		文件字节数组转为二维码字节数组
	**/
	public static void encodeBytes(String data, SortedMap<Integer,byte[]> qrcodes,Integer integer) {
		BitMatrix matrix = null;
		int h = 600;
		int w = 600;
		com.google.zxing.Writer writer = new QRCodeWriter();
		try {
			matrix = writer.encode(data,
					com.google.zxing.BarcodeFormat.QR_CODE, w, h);
		} catch (com.google.zxing.WriterException e) {
			System.out.println(e.getMessage());
		}
		try {
			ByteOutputStream bos = new ByteOutputStream();
			MatrixToImageWriter.writeToStream(matrix, "PNG",bos);
			byte[] res = bos.getBytes();
			bos.close();
			qrcodes.put(integer,res);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
 
	// qrcode 解码
	public static void decode(String file) {
		try {
			Result result = null;
			BufferedImage image = null;
			image = ImageIO.read(new File(file));
			LuminanceSource source = new BufferedImageLuminanceSource(image);
			BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
			Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
			hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
			result = new MultiFormatReader().decode(bitmap, hints);
			String rtn = result.getText();
			System.out.println(rtn);
			System.out.println(rtn.length());
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
	}
}

